﻿using System.Data;
using System.Linq;
using CH.FourBoards.LagerWalti.Service.MasterData;
using CH.FourBoards.LagerWalti.WebFrontEnd.View.Generic.RecordSetView;

namespace CH.FourBoards.LagerWalti.WebFrontEnd.View
{
    public class ProductList : RecordSetBase
    {

        protected override DataTable CreateRecordSetSource()
        {
            // prepare table
            var table = new DataTable();
            table.Columns.Add("RecordUrl");
            table.Columns.Add("Id");
            table.Columns.Add("CategoryName");
            table.Columns.Add("CategoryLink");
            table.Columns.Add("Name");
            table.Columns.Add("Bundle");
            table.Columns.Add("RowAttributes");

            // load data
            var service = new MasterDataService();
            var catalog = service.ListProducts();

            // iterate transactions
            foreach (var product in catalog.Products) {

                // locate data
                var category = catalog.Categories.First(c => c.Id == product.CategoryId);

                // add data to row
                var row = table.NewRow();
                row["RecordUrl"] = "#"; // ApplicationRoot + new CatagoryView().CreateCategoryLink(template.ID);
                row["Id"] = product.Id;
                row["CategoryName"] = category.Name;
                row["CategoryLink"] = "#"; // ApplicationRoot + new TemplateView().CreateTemplateLink(product.CategoryId);
                row["Name"] = product.Name;
                row["Bundle"] = string.Format("x{0}", product.BundleSize);
                row["RowAttributes"] = string.Empty;

                table.Rows.Add(row);
            }

            return table;
        }
    }
}